//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ZigguratRng.h"
using namespace Cephei::QL::Experimental::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phZigguratRng = NULL;
#endif
        _ppZigguratRng = new boost::shared_ptr<QuantLib::ZigguratRng> (new QuantLib::ZigguratRng ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (boost::shared_ptr<QuantLib::ZigguratRng>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZigguratRng = NULL;
#endif
	_ppZigguratRng = &childNative;
    
}
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (QuantLib::ZigguratRng& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phZigguratRng = NULL;
#endif
	_ppZigguratRng = new boost::shared_ptr<QuantLib::ZigguratRng> (&childNative);
    
    _ZigguratRngOwner = owner;
    
}

Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (CZigguratRng^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZigguratRng = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppZigguratRng = new boost::shared_ptr<QuantLib::ZigguratRng> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZigguratRng = NULL;
#endif
	if (!t->IsSubclassOf(CZigguratRng::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (QuantLib::Handle<QuantLib::ZigguratRng>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phZigguratRng = &childNative;
	_ppZigguratRng = &static_cast<boost::shared_ptr<QuantLib::ZigguratRng>>(childNative.currentLink());
    
    _ZigguratRngOwner = owner;
}
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (QuantLib::Handle<QuantLib::ZigguratRng> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phZigguratRng = &childNative;
	_ppZigguratRng = &static_cast<boost::shared_ptr<QuantLib::ZigguratRng>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Math::CZigguratRng::CZigguratRng (QuantLib::ZigguratRng childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZigguratRng = NULL;
#endif
	_ppZigguratRng = new boost::shared_ptr<QuantLib::ZigguratRng> (new QuantLib::ZigguratRng (childNative));
    
}
#endif

Cephei::QL::Experimental::Math::CZigguratRng::~CZigguratRng ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppZigguratRng != NULL)
    {
	    delete _ppZigguratRng;
        _ppZigguratRng = NULL;
    }
}
Cephei::QL::Experimental::Math::CZigguratRng::!CZigguratRng ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppZigguratRng != NULL)
    {
	    delete _ppZigguratRng;
    }
}
QuantLib::ZigguratRng& Cephei::QL::Experimental::Math::CZigguratRng::GetReference ()
{
    if (_ppZigguratRng == NULL) throw REFNEW NativeNullException ();
	return **_ppZigguratRng;
}
boost::shared_ptr<QuantLib::ZigguratRng>& Cephei::QL::Experimental::Math::CZigguratRng::GetShared ()
{
    if (_ppZigguratRng == NULL) throw REFNEW NativeNullException ();
	return *_ppZigguratRng;
}
QuantLib::ZigguratRng* Cephei::QL::Experimental::Math::CZigguratRng::GetPointer ()
{
    if (_ppZigguratRng == NULL) throw REFNEW NativeNullException ();
	return &**_ppZigguratRng;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ZigguratRng>& Cephei::QL::Experimental::Math::CZigguratRng::GetHandle ()
{
	if (_phZigguratRng == NULL)
	{
		_phZigguratRng = new Handle<QuantLib::ZigguratRng> (*_ppZigguratRng);
	}
	return *_phZigguratRng;
}
#endif
bool Cephei::QL::Experimental::Math::CZigguratRng::HasNative () 
{
	return (_ppZigguratRng != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Math::IZigguratRng^ Cephei::QL::Experimental::Math::CZigguratRng_Factory::Create ()
{
    return REFNEW CZigguratRng ();
}
